<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Eleven
*/

get_header(); ?>

<div id="primary">

	
	
	<?php

	$post_type = 'widgets';
	
	$sort = $_GET['sr'];
	$subj = $_GET['su'];
	
	
	if ($sort != '' || $subj != '') {

		
		$posts_per_page = get_query_var('posts_per_page');
		$paged = intval(get_query_var('paged'));
		
		$paged = ($paged) ? $paged : 1;
		$args = array(
			'post_type' => $post_type,
			'posts_per_page' => $posts_per_page,
			'paged' => $paged,
			'more' => $more = 0
		);
		if ($sort != '') {
			if ($sort != 'comments') {
				$args['meta_key'] = $sort;
				$args['orderby'] = 'meta_value_num';
				$args['order'] = 'DESC';
			} else {
				$args['orderby'] = 'comment_count';
				$args['order'] = 'DESC';
			}
		}
		if ($subj != '') {
			$args['taxonomy'] ='category';
			$args['term'] = $subj;
		}
		query_posts($args);	
		
	}
	?>
	
	<?php if ( have_posts() ) : ?>
	
	<div id="content" role="main"><div>
	
		<?php  
		$index = 1; 
		while ( have_posts() ) : the_post();
			
			// `get primary meta content
			$bad_count = 0;
			$bad_str = '';
			$post_id = get_the_ID();
			$preview_image = get_post_meta($post_id, 'wpcf-widget-preview-image', true);
			$description = get_post_meta($post_id, 'wpcf-widget-description', true);
			$content = get_post_meta($post_id, 'wpcf-widget-content', true);
			
			$the_title = get_the_title();
			if ($the_title == '') {
				$bad_str .=  '- Item title not found ';
				$bad_count++;
			}
			else if (strlen($the_title) <= 10) {
				$bad_str .=  '- Item title too short (<=10) ';
				$bad_count++;
			}
			else if (strpos($the_title, ' ') === false) {
				$bad_str .=  '- Item title needs more words ';
				$bad_count++;
			}
			
			
			if (!is_image($preview_image)) {
				$bad_count++;
				$bad_str .= '- Wrong item preview image src ';
			} else if (strpos($preview_image, esc_url(home_url()) . '/wp-content/uploads/') === false) {
				$bad_count++;
				$bad_str .= '- Item preview image must under '. esc_url(home_url()).' (use upload) ';
			}
				
			if ($description == '') {
				$bad_str .=  '- Item description not found ';
				$bad_count++;
			} else if (strlen($description) <= 150) {
				$bad_str .=  '- Item description too short (<=150) ';
				$bad_count++;
			} else if (strpos($description, ' ') === false) {
				$bad_str .=  '- Item description need more words ';
				$bad_count++;
			}
			
			if ($content == '') {
				$bad_str .=  '- Widget has not content';
				$bad_count++;
			} else if (strlen($content) <= 50) {
				$bad_str .=  '- Widget content too short (<=50) ';
				$bad_count++;
			}
			
			$thumb = timthumb($preview_image);
			?>
			
			
			
			<?php if ($bad_count == 0) :
			// `show items of index ?>
			<div class="item">
				<a href="<?php echo get_permalink();?>" class="thumb" title="<?php echo get_the_title();?>">
					<img src="<?php echo $thumb; ?>" alt="<?php echo get_the_title();?> thumb" width="180" height="150"/>
				</a>
				<h2 class="title">
					<a href="<?php echo get_permalink();?>" class="thumb" title="<?php echo get_the_title();?>">
						<?php the_title();?>
					</a>
				</h2>
				<?php if ($sort != ''): ?>
				<span class="meta"><?php
					if ($sort != 'comments') { 
						$meta = get_post_meta($post_id, $sort, true);
						if ($meta == '') $meta = 0;
						$meta = (int) $meta;
						echo $meta . ' ' . $sort;
					} else {
						echo get_comments_number() . ' comments';
					}
				?></span>
				<?php endif; ?> 
			</div>
			<?php else:?>
				<div class="item bad-item" title="<?php echo $bad_str;?>">
					<div class="bad-item-holder">
					<?php edit_post_link();?>
					</div>
					<span class="bad-item-title">Bad item</span>
				</div>
				
			<?php endif;?>
			
			
			
			
			<?php 
				if ($index % 3 == 0) echo '<div class="clear"></div><hr/>';
				$index++;
			?>
			
		<?php endwhile; ?>
		<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
	<div class="clear"></div>
	</div></div><!-- #content -->
	<?php get_sidebar(); ?>
	
	<?php else : ?>
	
		<div id="content" role="main"><div>
		<article id="post-0" class="post no-results not-found">
			<header class="entry-header">
				<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
			</header><!-- .entry-header -->

			<div class="entry-content">
				<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
				<?php get_search_form(); ?>
			</div><!-- .entry-content -->
		</article><!-- #post-0 -->
		</div></div><!-- #content -->
	<?php endif; ?>
	
	
	
	
	
	
	
</div><!-- #primary -->


<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/index.js"></script>


<?php get_footer(); ?>